package com.practice.selenium.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Predicate;
import com.practice.selenium.driver.WebDriverFactory;

public class SearchImageTestCase implements TestCase {

	private static final String HOME_URL = "http://cn.bing.com/";
	private static final String IMAGE_BUTTON_XPATH = "//*[@id=\"scpl0\"]";
	private static final String SEARCH_BUTTON_XPATH = "//*[@id=\"sb_form_go\"]";
	private static final String SEARCH_INPUT_XPATH = "//*[@id=\"sb_form_q\"]";
	
	private static final String SEARCH_IMAGE = "Search Image";
	
	@Override
	public void test() {
		WebDriver driver = WebDriverFactory.getInstance().createWebDriver();
		driver.get(HOME_URL);
		
		System.out.println(driver.getTitle());
		
		WebElement imageButton = driver.findElement(By.xpath(IMAGE_BUTTON_XPATH));
		imageButton.click();
		WebDriverWait wait = new WebDriverWait(driver, 5);
		Predicate<WebDriver> predicateTitle = webDriver -> webDriver.getCurrentUrl().contains("image"); 
		wait.until(predicateTitle);
		
		System.out.println(driver.getTitle());
		
		WebElement inputTextbox = driver.findElement(By.xpath(SEARCH_INPUT_XPATH));
		inputTextbox.sendKeys(SEARCH_IMAGE);
		
		WebElement searchButton = driver.findElement(By.xpath(SEARCH_BUTTON_XPATH));
		searchButton.click();
		
		driver.quit();
		
	}

}
